using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Windows.Documents;
using System.Windows.Input;

namespace Bull.Core {
    internal class CommandManagerHelper {
        internal static void CallWeakReferenceHandlers(List<WeakReference> handlers) {
            if(handlers != null) {
                var callees = new EventHandler[handlers.Count];
                int count = 0;

                for(var i = handlers.Count - 1; i >= 0; i--) {
                    WeakReference reference = handlers[i];
                    var handler = reference.Target as EventHandler;
                    if(handler == null) {
                        handlers.RemoveAt(i);
                    }
                    else {
                        callees[count] = handler;
                        count++;
                    }
                }

                for(int i = 0; i < count; i++) {
                    EventHandler handler = callees[i];
                    handler(null, EventArgs.Empty);
                }
            }
        }

        internal static void AddHandlersToRequerySuggested(List<WeakReference> handlers) {
            if(handlers != null) {
                foreach(WeakReference handlerRef in handlers) {
                    var handler = handlerRef.Target as EventHandler;
                    if(handler != null) {
                        CommandManager.RequerySuggested += handler;
                    }
                }
            }
        }

        internal static void RemoveHandlersFromRequerySuggested(List<WeakReference> handlers) {
            if(handlers != null) {
                foreach(WeakReference handlerRef in handlers) {
                    var handler = handlerRef.Target as EventHandler;
                    if(handler != null) {
                        CommandManager.RequerySuggested -= handler;
                    }
                }
            }
        }

        internal static void AddWeakReferenceHandler(ref List<WeakReference> handlers, EventHandler handler) {
            AddWeakReferenceHandler(ref handlers, handler, -1);
        }

        internal static void AddWeakReferenceHandler(ref List<WeakReference> handlers, EventHandler handler,
            int defaultListSize) {
            if(handlers == null) {
                handlers = (defaultListSize > 0 ? new List<WeakReference>(defaultListSize) : new List<WeakReference>());
            }

            handlers.Add(new WeakReference(handler));
        }

        internal static void RemoveWeakReferenceHandler(List<WeakReference> handlers, EventHandler handler) {
            if(handlers != null) {
                for(int i = handlers.Count - 1; i >= 0; i--) {
                    WeakReference reference = handlers[i];
                    var existingHandler = reference.Target as EventHandler;
                    if((existingHandler == null) || (existingHandler == handler)) {
                        handlers.RemoveAt(i);
                    }
                }
            }
        }
    }
}